# coding: utf-8
"""
@python version : python3.10
@file name      : app06.py
@date           : 2024/5/15 15:15
@author         : ziyang.yang@aliyun.com
@gitee          : https://gitee.com/ziyangyang1318
@blog           : blog.yangziyang.top
@describe       : 响应模型参数据
"""

from typing import Union

from fastapi import APIRouter
from pydantic import BaseModel, EmailStr
from typing import List

app06 = APIRouter()


# 请求模型，请求数据的时候，按下面字段进行数据请求
class UserIn(BaseModel):
    username: str
    password: str
    email: EmailStr
    full_name: Union[str, None] = None


# 响应模型, 返回数据时，按照下面字段进行数据返回,可以自定义返回数据字段
class UserOut(BaseModel):
    username: str
    email: EmailStr
    full_name: Union[str, None] = None


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: float = 10.5
    tags: List[str] = []


# 定义数据库里的数据
items = {
    "foo": {"name": "Foo", "price": 50.2},
    "bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
    "baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
}


# @app06.get("/items/{item_id}", response_model=Item, response_model_exclude_unset=True)
# async def read_item(item_id: str):
#     """
#     :param item_id:
#     :return: 在设置了response_model_exclude_unset=True，
#     返回的数据是根据数据库原有的数据返回
#     """
#     return items[item_id]


@app06.post("/user/", response_model=UserOut)
async def create_user(user: UserIn):
    return user


# response_model_exclude
@app06.get("/items/{item_id}", response_model=Item, response_model_exclude={"description"}, )
async def read_item(item_id: str):
    return items[item_id]


# response_model_include
@app06.get("/items/{item_id}", response_model=Item, response_model_include={"name", "price"}, )
async def read_item(item_id: str):
    return items[item_id]
